from django.contrib.auth.models import User
from django.db import models


# Create your models here.
# 继承模型类之后 普通类才能成为模型类
class Classes(models.Model):
    # 添加id字段 设置为自增长约束 设置为主键约束
    id = models.AutoField(primary_key=True)
    # 添加name字段 设置字段最长约束为30 默认会有非空约束
    name = models.CharField(max_length=30, unique=True)

    def __str__(self):
        return self.name


class Label(models.Model):
    name = models.CharField(max_length=30, unique=True)

    def __str__(self):
        return 'Label<name=%s>' % self.name


class UserDetail(models.Model):
    age = models.IntegerField(null=True, blank=True)
    sex = models.BooleanField(null=True, blank=True)
    user = models.OneToOneField(User, on_delete=models.CASCADE)


class Article(models.Model):
    title = models.CharField(max_length=30)
    desc = models.CharField(max_length=100)
    content = models.TextField()
    cover=models.TextField()
    create_time=models.DateTimeField(auto_now_add=True)
    update_time=models.DateTimeField(auto_now=True)
    user=models.ForeignKey(User,on_delete=models.CASCADE)
    classes=models.ForeignKey(Classes,on_delete=models.NOT_PROVIDED)
    label=models.ManyToManyField(Label)

    def __str__(self):
        return 'Article<name=%s>' % self.title
